﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO_MechSys;
using BLL_MechSys;

namespace FORMS_MechSys
{
    public partial class frmServicoAdicionar : Form
    {
        /// <summary>
        /// Variaveis
        /// </summary>
        #region
        int OrdemPendenteID;

        DataTable dataTableGridView = new DataTable();
        List<DTOServico> ListServicos = new List<DTOServico>();


        bool Edicao = false;
        DTOOrdemDetalheServico dtoOrdemDetalheServicoEd = new DTOOrdemDetalheServico();
        DTOServico dtoServicoADD = new DTOServico();
        BLLServico bllServico = new BLLServico();
        BLLOrdemDetalheServico bllOrdemDetalheServico = new BLLOrdemDetalheServico();
        BLLFornecedor bllFornecedor = new BLLFornecedor();
        BLLCategoria bllCategoria = new BLLCategoria();
        DTOCor dtoCorPrincipal = new DTOCor();
        DTOCor dtoCorLetra = new DTOCor();
        DTOCor dtoCorFundo = new DTOCor();
        DTOCor dtoCorDataGrid = new DTOCor();
        BLLCor bllCor = new BLLCor();

        
        

        #endregion

        public frmServicoAdicionar(int ordemPendenteID)
        {
            InitializeComponent();
            PopulaGrid();
            SetColors();
            OrdemPendenteID = ordemPendenteID;
            btnAdicionar.Text = "Adicionar";
        }

        public frmServicoAdicionar(DTOOrdemDetalheServico dtoOrdemDetalheServico)
        {
            InitializeComponent();
            PopulaGrid();
            SetColors();
            OrdemPendenteID = dtoOrdemDetalheServico.OrdemPendenteID;
            Edicao = true;
            btnAdicionar.Text = "Salvar";

            txtCodigo.Text = dtoOrdemDetalheServico.ServicoID.ToString();
            List<DTOServico> ListDtoServico  = bllServico.PesquisarServicos("", dtoOrdemDetalheServico.ServicoID);
            DTOServico dtoservico = new DTOServico();
            if(ListDtoServico.Count > 0)
                dtoservico = ListDtoServico[0];
            txtDescricao.Text = dtoservico.Descricao;
            txtValor.Text = string.Format("{0:0.00}", dtoOrdemDetalheServico.Valor);
            if (new BLLFuncionario().PesquisarFuncionarios("", dtoOrdemDetalheServico.FuncionarioID).Count > 0)
            {
                DTOFuncionario dtofuncionario = new BLLFuncionario().PesquisarFuncionarios("", dtoOrdemDetalheServico.FuncionarioID)[0];
                if (dtoOrdemDetalheServico.FuncionarioID == dtofuncionario.FuncionarioID)
                    txtFuncionario.Text = dtofuncionario.Nome + " " + dtofuncionario.Sobrenome;
                BLLUtilitarios.funcionarioID = new int();
                BLLUtilitarios.funcionarioNome = "";
            }
            dtoOrdemDetalheServicoEd = dtoOrdemDetalheServico;
        }

        public void PopulaGrid()
        {
            DataRow dataRow;
            dataTableGridView = new DataTable();
            List<DTOServico> ListServicoTemp;

            ListServicoTemp = bllServico.BuscarServicosCadastradosLinha();

            if (ListServicos != ListServicoTemp)
            {
                ListServicos = ListServicoTemp;
                if (ListServicos.Count > 0)
                {
                    dataTableGridView.Columns.Add("ServicoID", typeof(int));
                    dataTableGridView.Columns.Add("Descrição", typeof(string));
                    dataTableGridView.Columns.Add("Valor", typeof(double));

                    foreach (DTOServico dtoServico in ListServicos)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["ServicoID"] = dtoServico.ServicoID;
                        dataRow["Descrição"] = dtoServico.Descricao;
                        dataRow["Valor"] = dtoServico.ValorSugerido;

                        // Adiciona linha ao DataTable.
                        dataTableGridView.Rows.Add(dataRow);
                    }
                }
                dtGrid.DataSource = null;
                dtGrid.DataSource = dataTableGridView;
            }
        }

        public void PopulaGrid(string descricao, int codigo)
        {
            DataRow dataRow;
            dataTableGridView = new DataTable();
            List<DTOServico> ListServicoTemp;

            ListServicoTemp = bllServico.BuscarServicosCadastradosLinha();//(descricao, codigo);


            if (ListServicos != ListServicoTemp)
            {
                ListServicos = ListServicoTemp;
                if (ListServicos.Count > 0)
                {
                    dataTableGridView.Columns.Add("ServicoID", typeof(int));
                    dataTableGridView.Columns.Add("Descrição", typeof(string));
                    dataTableGridView.Columns.Add("Valor", typeof(double));

                    foreach (DTOServico dtoServico in ListServicos)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["ServicoID"] = dtoServico.ServicoID;
                        dataRow["Descrição"] = dtoServico.Descricao;
                        dataRow["Valor"] = dtoServico.ValorSugerido;

                        // Adiciona linha ao DataTable.
                        dataTableGridView.Rows.Add(dataRow);
                    }
                }
                dtGrid.DataSource = null;
                dtGrid.DataSource = dataTableGridView;
            }
        }

        private void SetColors()
        {
            //
            //
            dtoCorPrincipal = bllCor.Buscar("Principal");
            btn_pesquisar.BackColor = btnAdicionar.BackColor = lblTitulo.BackColor = tlpBarraTop.BackColor = btnBuscarFuncionario.BackColor =
                toolStrip1.BackColor = toolStripLogo.BackColor = dtoCorPrincipal.Cor;
            //

            //
            //
            dtoCorFundo = bllCor.Buscar("Fundo");
            tlpPrincipal.BackColor = dtoCorFundo.Cor;
            //

            //
            //
            dtoCorDataGrid = bllCor.Buscar("DataGrid");
            dtGrid.BackgroundColor = dtoCorDataGrid.Cor;
            //


            //
            //
            dtoCorLetra = bllCor.Buscar("Letra");
            btnAdicionar.ForeColor = btn_pesquisar.ForeColor = label1.ForeColor = label2.ForeColor =
                label3.ForeColor = label4.ForeColor = btnBuscarFuncionario.ForeColor =
                label8.ForeColor = lblTitulo.ForeColor =
            dtoCorLetra.Cor;

            //
        }

        private void Limpar()
        {
            txtCodigo.Clear();
            txtDescricao.Clear();
            txtPesquisar.Clear();
            txtValor.Clear();
            txtFuncionario.Clear();
            txtPesquisar.Focus();

        }

        private void btn_pesquisar_Click(object sender, EventArgs e)
        {
            int cod = new int();

            Int32.TryParse(txtPesquisar.Text, out cod);
            PopulaGrid("%" + txtPesquisar.Text + "%", cod);
        }

        private void toolStripButtonSair_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdicionar_Click(object sender, EventArgs e)
        {
            DTOOrdemDetalheServico dtoOrdemDetalheServico = new DTOOrdemDetalheServico();
            if (!string.IsNullOrWhiteSpace(txtCodigo.Text) &&
                !string.IsNullOrWhiteSpace(txtValor.Text))
            {
                dtoOrdemDetalheServico.Valor = double.Parse(txtValor.Text);
                dtoOrdemDetalheServico.ServicoID = int.Parse(txtCodigo.Text);
                dtoOrdemDetalheServico.OrdemPendenteID = OrdemPendenteID;

                DTOServico dtoServico = new BLLServico().PesquisarServicosLinha("", dtoOrdemDetalheServico.ServicoID)[0];
                if (dtoServico.ValorSugerido > dtoOrdemDetalheServico.Valor)
                {
                    dtoOrdemDetalheServico.Desconto = dtoServico.ValorSugerido - dtoOrdemDetalheServico.Valor;
                }
                dtoOrdemDetalheServico.FuncionarioID = BLLUtilitarios.funcionarioID;
                if (!Edicao)
                {
                    bllOrdemDetalheServico.Inserir(dtoOrdemDetalheServico);
                }
                else
                {
                    dtoOrdemDetalheServicoEd.Valor = double.Parse(txtValor.Text);
                    dtoOrdemDetalheServicoEd.ServicoID = int.Parse(txtCodigo.Text);
                    dtoOrdemDetalheServicoEd.OrdemPendenteID = OrdemPendenteID;

                    dtoServico = new BLLServico().PesquisarServicosLinha("", dtoOrdemDetalheServicoEd.ServicoID)[0];
                    if (dtoServico.ValorSugerido > dtoOrdemDetalheServicoEd.Valor)
                    {
                        dtoOrdemDetalheServico.Desconto = dtoServico.ValorSugerido - dtoOrdemDetalheServicoEd.Valor;
                    }
                    dtoOrdemDetalheServicoEd.FuncionarioID = BLLUtilitarios.funcionarioID;
                    bllOrdemDetalheServico.Atualizar(dtoOrdemDetalheServicoEd);
                    this.Close();
                }
                Limpar();
                PopulaGrid();
            }
            else
            {
                new frmMessageBox().Show("Existem campos obrigatórios em Branco.");
            }

        }

        private void txtPesquisar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_pesquisar_Click(null, null);
        }

        private void dtGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtCodigo.Text = dtGrid["ServicoID", dtGrid.CurrentCellAddress.Y].Value.ToString();
            txtDescricao.Text = dtGrid["Descrição", dtGrid.CurrentCellAddress.Y].Value.ToString();
            txtValor.Text = string.Format("{0:0.00}", (dtGrid["Valor", dtGrid.CurrentCellAddress.Y].Value.ToString()));

            txtValor.Focus();
        }

        private void btnBuscarFuncionario_Click(object sender, EventArgs e)
        {
            new frmSelecionarGenerico("Funcionário").ShowDialog();
            txtFuncionario.Text = BLLUtilitarios.funcionarioNome;

        }
    }
}
